require "ui.gooi"

local UIManager = class("UIManager")

function UIManager:ctor()
	self.windows = {}
	
end

function UIManager:getWindow(name)
	for n, window in pairs(self.windows) do 
		if n==name then 
			return window
		end
	end
	return nil
end

function UIManager:createWindow(name, ...)

	local window = self:getWindow(name)
	if window then 
		return window
	end

	local ok, Class = pcall(require,"ui.windows."..name)
	-- print("____win____",winClass)
	if not Class then 
		return 
	end
	local window = Class.new()	
	window:init(...)
	self.windows[name] = window
	return window
end

function UIManager:showWindow(name)
	local window = self:getWindow(name)
	if window then 
		window:show()
	end	
end

function UIManager:hideWindow(name)
	local window = self:getWindow(name)
	if window then 
		window:hide()
	end	
end

function UIManager:closeWindow(name)
	for n, window in pairs(self.windows) do 
		if n==name then 
			self.windows[name] = nil
			return
		end
	end	

end

function UIManager:draw()
    gooi.draw() 
end

function UIManager:update(dt)
    gooi.update(dt) 
	for n, window in pairs(self.windows) do 
		window:update(dt)
	end	    
end

function UIManager:mousereleased(x, y, button)
	gooi.released() 
end

function UIManager:mousepressed(x, y, button)
	gooi.pressed() 
end

function UIManager:textinput(text)
    gooi.textinput(text)
end

function UIManager:keypressed(key, scancode, isrepeat)
    gooi.keypressed(key, scancode, isrepeat)

    if key == "escape" then
        love.event.quit()
    end
end

function UIManager:keyreleased(key, scancode)
    gooi.keyreleased(key, scancode)
end


return UIManager